#include "time.h"
void tim6Init()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
	//基本定时器
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 10000;//装载值，计数次数,一个计时周期为10ms
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84 -1;//预分频，基础频率为84MHz
	TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct);//基本定时器初始化
	
	TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);	//设置中断源
	NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn;//设置中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//中断使能
	NVIC_Init(&NVIC_InitStruct);//定时器中断初始化
	
	TIM_Cmd(TIM6, ENABLE);//基本定时器开启
}


void tim1Init()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	GPIO_InitTypeDef GPIO_InitStruct;
	//定时器和GPIO复用为TIM1功能时钟使能
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	
	//IO口初始化
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStruct);
	//指定IO口的具体复用功能
	GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_TIM1);
	
	//初始化TIM1高级定时器的基本定时功能
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割（输入才有）
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 1000;//装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;//分频
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
	
	//初始化输出比较功能
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//向上计数时，小于参考值为有效
	TIM_OCInitStruct.TIM_Pulse = 0;//CCR寄存器的值，占空比
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;//输出通道低电平有效
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Disable;//输出通道使能
	//------------以下为高级定时器才用到，通用定时器不需要配置
	TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set;//空闲状态下输出的电平
	TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Set;//互补端空闲状态下输出的电平
	TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_Low;//互补端输出通道低电平有效
	TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Enable;//互补端输出通道使能
	TIM_OC2Init(TIM1, &TIM_OCInitStruct);
	/*
	每当发生 N+1 个计数器上溢或下溢（其中，N 是 TIMx_RCR 重复计数器寄存器 中的值），
	数据就将从预装载寄存器转移到影子寄存器（TIMx_ARR 自动重载寄存器、 
	TIMx_PSC 预分频器寄存器以及比较模式下的 TIMx_CCRx 捕获/比较寄存器）。
	*/
	//开启外设预加载寄存器寄存器，当参考值做出变化时，下一轮才生效
	TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);	//CCR（比较/捕获寄存器）
	TIM_ARRPreloadConfig(TIM1, ENABLE);	//ARR（自动重载寄存器）
	//开启输出比较寄存器
	TIM_CtrlPWMOutputs(TIM1, ENABLE);
	TIM_Cmd(TIM1, ENABLE);//高级定时器1启动
}
